package DAL;

import Entities.TblMarks;
import DatabaseAccess.SQLHelper;
import java.sql.ResultSet;
import java.util.*;

public class MarksDAL {

    private ResultSet objResult;
    public List<TblMarks> list;

    public MarksDAL() {
    }

    public boolean add(TblMarks mark) {
        int result = 0;
        String[] params = new String[6];
        params[0] = String.valueOf(mark.getExamId());
        params[1] = mark.getStudentRollNo();
        params[2] = String.valueOf(mark.getTeacherId());
        params[3] = String.valueOf(mark.getReTest());
        params[4] = String.valueOf(mark.getMark());
        params[5] = mark.getRemark();

        try {
            result = SQLHelper.executeUpdate("ProcInsertMark", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean update(TblMarks mark) {
        int result = 0;
        String[] params = new String[7];
        params[0] = String.valueOf(mark.getMarkId());
        params[1] = String.valueOf(mark.getExamId());
        params[2] = mark.getStudentRollNo();
        params[3] = String.valueOf(mark.getTeacherId());
        params[4] = String.valueOf(mark.getReTest());
        params[5] = String.valueOf(mark.getMark());
        params[6] = mark.getRemark();

        try {
            result = SQLHelper.executeUpdate("procUpdateMark", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public boolean updateSimple(TblMarks mark) {
        int result = 0;
        String[] params = new String[2];
        params[0] = String.valueOf(mark.getMarkId());
        params[1] = String.valueOf(mark.getMark());

        try {
            result = SQLHelper.executeUpdate("procUpdateMarkSimple", params);
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
        }

        return result == 1 ? true : false;
    }

    public List<TblMarks> getAll() {
        list = new ArrayList<TblMarks>();
        try {
            objResult = SQLHelper.executeQuery("procGetAllMark", null);
            while (objResult.next()) {
                TblMarks mark = new TblMarks();
                mark.setMarkId(objResult.getInt("MarkId"));
                mark.setExamId(objResult.getInt("ExamId"));
                mark.setStudentRollNo(objResult.getString("StudentRollNo"));
                mark.setTeacherId(objResult.getInt("TeacherId"));
                mark.setReTest(objResult.getInt("ReTest"));
                mark.setMark(objResult.getFloat("Mark"));
                mark.setRemark(objResult.getString("Remark"));

                list.add(mark);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public TblMarks getById(int id) {
        TblMarks mark = new TblMarks();
        String[] params = new String[1];
        params[0] = String.valueOf(id);
        try {
            objResult = SQLHelper.executeQuery("procGetSubjectById", params);
            while (objResult.next()) {
                mark.setMarkId(objResult.getInt("MarkId"));
                mark.setExamId(objResult.getInt("ExamId"));
                mark.setStudentRollNo(objResult.getString("StudentRollNo"));
                mark.setTeacherId(objResult.getInt("TeacherId"));
                mark.setReTest(objResult.getInt("ReTest"));
                mark.setMark(objResult.getFloat("Mark"));
                mark.setRemark(objResult.getString("Remark"));
            }

            return mark;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
    
    public List<TblMarks> getMarksByStudentRollNo(String studentRollNo) {
        list = new ArrayList<TblMarks>();
        String[] params = new String[1];
        params[0] = studentRollNo;
        try {
            objResult = SQLHelper.executeQuery("procGetMarkByStudentRollNo", params);
            while (objResult.next()) {
                TblMarks mark = new TblMarks();
                mark.setMarkId(objResult.getInt("MarkId"));
                mark.setExamId(objResult.getInt("ExamId"));
                mark.setStudentRollNo(objResult.getString("StudentRollNo"));
                mark.setTeacherId(objResult.getInt("TeacherId"));
                mark.setReTest(objResult.getInt("ReTest"));
                mark.setMark(objResult.getFloat("Mark"));
                mark.setRemark(objResult.getString("Remark"));

                list.add(mark);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }

    public List<TblMarks> getMarksByExamId(int examId) {
        list = new ArrayList<TblMarks>();
        String[] params = new String[1];
        params[0] = String.valueOf(examId);
        
        try {
            objResult = SQLHelper.executeQuery("procGetMarkByExamId", params);
            while (objResult.next()) {
                TblMarks mark = new TblMarks();
                mark.setMarkId(objResult.getInt("MarkId"));
                mark.setExamId(objResult.getInt("ExamId"));
                mark.setStudentRollNo(objResult.getString("StudentRollNo"));
                mark.setTeacherId(objResult.getInt("TeacherId"));
                mark.setReTest(objResult.getInt("ReTest"));
                mark.setMark(objResult.getFloat("Mark"));
                mark.setRemark(objResult.getString("Remark"));

                list.add(mark);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
    
    public List<TblMarks> getMarksByClassIdAndExamId(int classId, int examId) {
        list = new ArrayList<TblMarks>();
        String[] params = new String[2];
        params[0] = String.valueOf(classId);
        params[1] = String.valueOf(examId);
        try {
            objResult = SQLHelper.executeQuery("procGetMarkByClassAndExam", params);
            while (objResult.next()) {
                TblMarks mark = new TblMarks();
                mark.setMarkId(objResult.getInt("MarkId"));
                mark.setExamId(objResult.getInt("ExamId"));
                mark.setStudentRollNo(objResult.getString("StudentRollNo"));
                mark.setTeacherId(objResult.getInt("TeacherId"));
                mark.setReTest(objResult.getInt("ReTest"));
                mark.setMark(objResult.getFloat("Mark"));
                mark.setRemark(objResult.getString("Remark"));

                list.add(mark);
            }

            return list;
        } catch (Exception ex) {
            System.out.println("Error: " + ex.getMessage());
            return null;
        }
    }
}
